package com.goldcn.user.service;

import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.IdGen;
import com.goldcn.config.ProConstants;
import com.goldcn.enums.ErpInterfaceCfgEnum;
import com.goldcn.interceptor.SystemContext;
import com.goldcn.user.ErpDto.ClientDto;
import com.goldcn.user.ErpDto.ResultData;
import com.goldcn.user.ErpDto.UnitDto;
import com.goldcn.user.dao.CompanyInfoMapper;
import com.goldcn.user.dao.OrganizationalMapper;
import com.goldcn.user.model.CompanyInfo;
import com.goldcn.user.model.Organizational;
import com.goldcn.util.CommonTools;
import com.goldcn.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/28.
 */
@Service
public class ErpInterfaceService {
    @Autowired
    OrganizationalMapper organizationalMapper;
    @Autowired
    CompanyInfoMapper companyInfoMapper;
    @Autowired
    RestTemplate restTemplate;




    /**
     * 封装erp供应商的参数  0表示新增 1表示修改(我们这边审核或者修改)
     */
    public Object addOrUpdateCompanyForErp(CompanyInfo info,int type){
        ClientDto clientDto = new ClientDto();
        clientDto.setClientID(info.getCompanyInfoID()+"");
        clientDto.setClientName(info.getCompanyName());
        //clientDto.setClientType(1);
        clientDto.setClientType(info.getType());//1供应商,2客户(采购商),3客商
        clientDto.setSimpleName(info.getShortName());
        clientDto.setProvince(info.getRegProvince());
        clientDto.setCity(info.getRegCity());
        clientDto.setDistrict(info.getRegDistrict());
        clientDto.setContactMobile(info.getContactPhone());
        clientDto.setContactName(info.getContacts());
        clientDto.setIsStop(0);

        clientDto.setAddTime(System.currentTimeMillis());
//        if(type==0){
        clientDto.setAccountCode(CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.ACCOUNTCODE.getCode()));
//        }

        clientDto.setLoginName(info.getAccount());
        clientDto.setIsJoint(1);
       //这里是调用
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("secretKey", CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.SECRETKEY.getCode()));
        RestTemplate ts = new RestTemplate();
        ResponseEntity<ResultData> result = null;
        HttpEntity<ClientDto> entity = new HttpEntity<ClientDto>(clientDto,headers);
        if(0==type){
            //表示新增
            result =  ts.exchange(CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.ADDCOMPANYURL.getCode()), HttpMethod.POST, entity, ResultData.class, "");
        }else{
            result =  ts.exchange(CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.UPDATECOMPANYURL.getCode()), HttpMethod.POST, entity, ResultData.class, "");
        }
        ResultData res =  result.getBody();
        String code = res.getCode();
        if("1".equals(code)){
            //成功
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 初始化公司信息 -->除了自营的公司 代销的公司全都抛转到1002下面
     * @return
     */
    public Object companyMainData(){
        List<CompanyInfo> companyInfoList = companyInfoMapper.selectAllCompany(SystemContext.getCompanyID());
        ClientDto []clientDtos = new ClientDto[companyInfoList.size()];
        for(int i=0;i<companyInfoList.size();i++){
            CompanyInfo info = companyInfoList.get(i);
            ClientDto clientDto = new ClientDto();
            clientDto.setClientID(info.getCompanyInfoID()+"");
            clientDto.setClientName(info.getCompanyName());
            clientDto.setClientType(1);
            clientDto.setSimpleName(info.getShortName());
            clientDto.setProvince(info.getRegProvince());
            clientDto.setCity(info.getRegCity());
            clientDto.setDistrict(info.getRegDistrict());
            clientDto.setContactMobile(info.getContactPhone());
            System.out.println(info.getContactPhone()+"*******");
            clientDto.setAccountCode(CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.ACCOUNTCODE.getCode()));
            clientDto.setContactName(info.getContacts());
            clientDto.setIsStop(0);
            if(info.getIsAudit()!=null){
                if(0==info.getIsAudit() || 2==info.getIsAudit()){
                    clientDto.setIsStop(1);
                    //增加的时候是0 未审核 到审核通过了则是通过或者失败
                }else{
                    clientDto.setIsStop(0);
                }
            }
            clientDto.setAddTime(System.currentTimeMillis());
            clientDto.setLoginName(info.getAccount());
            clientDto.setIsJoint(1);
            clientDtos[i] = clientDto;
        }

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("secretKey",CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.SECRETKEY.getCode()));
        RestTemplate ts = new RestTemplate();
        ResponseEntity<ResultData> result = null;
        HttpEntity<ClientDto[]> entity = new HttpEntity<ClientDto[]>(clientDtos,headers);
        result = ts.exchange(CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.MIANCOMPANY.getCode()), HttpMethod.POST, entity, ResultData.class, "");
        ResultData res =  result.getBody();
        String code = res.getCode();
        if("1".equals(code)){
            //成功
            System.out.println("初始化公司信息成功");
            return 1;
        }else{
            return 0;
        }

    }


    /**
     * 初始化组织
     * type: 0表示accountCode为1001  1 表示accountCode未1002
     * @return
     */
    public Object maintenceDateOrg(int type){
        ResultData<Map<String,String>> data_ext = HttpUtil.httpGet_ResultData(ProConstants.BANKOPSSERVICE + "/sysConfig/selectAllExtByCID?companyInfoID="+ SystemContext.getCompanyID(), restTemplate);
        Map<String,String> ext = new HashMap<String,String>();
        if("1".equals(data_ext.getCode())){
            ext = data_ext.getData();
        }
        System.out.print("ErpInterfaceCfgEnum.ACCOUNTCODE.getCode()==="+ErpInterfaceCfgEnum.ACCOUNTCODE.getCode());
        List<Organizational> orgs = organizationalMapper.getAll(SystemContext.getCompanyID());
        UnitDto[] unitDtos = new UnitDto[orgs.size()];
        for(int i=0;i<orgs.size();i++){
            Organizational org = orgs.get(i);
            UnitDto unitDto = new UnitDto();
            unitDto.setID(org.getoID()+"");
            unitDto.setType(1);
            unitDto.setIsStop(0);
            unitDto.setUnitNname(org.getOrganizationalName());
            unitDto.setAccountCode(ext.get(ErpInterfaceCfgEnum.ACCOUNTCODE.getCode()));
            unitDto.setIsJoint(1);
            if(type==0){
                unitDto.setAccountCode(ext.get(ErpInterfaceCfgEnum.ACCOUNTCODE.getCode()));
            }else{
                //unitDto.setAccountCode(ext.get(ErpInterfaceCfgEnum.ACCOUNTELSECODE.getCode()));
            }
            //剩下一个父极id
            int level = org.getLevel();
            switch (level){
                case 1:
                    unitDto.setpID(null);
                    break;
                case 2:
                    //得到总部id
                    unitDto.setpID(organizationalMapper.selectHeadCCB(SystemContext.getCompanyID()).getoID()+"");
                    break;
                case 3:
                    unitDto.setpID(org.getBranchID()+"");
                    break;
                case 4:
                    unitDto.setpID(org.getTwoLevelBranchID()+"");
                    break;
                default:
                    throw new ServiceException("传递的参数错误", RestConst.ErrorCode.FORM_FAIL);
            }
            unitDtos[i]=unitDto;
        }

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("secretKey",ext.get(ErpInterfaceCfgEnum.SECRETKEY.getCode()));
        RestTemplate ts = new RestTemplate();
        ResponseEntity<ResultData> result = null;
        HttpEntity<UnitDto[]> entity = new HttpEntity<UnitDto[]>(unitDtos,headers);
        result = ts.exchange(ext.get(ErpInterfaceCfgEnum.MAINORG.getCode()), HttpMethod.POST, entity, ResultData.class, "");
        ResultData res =  result.getBody();
        String code = res.getCode();
        if("1".equals(code)){
            //成功
            System.out.println("初始化组织信息成功");
            return 1;
        }else{
            return 0;
        }

    }


    /**
     * 新增或者修改架构信息
     * @return
     */
    public Object addOrUpdateOrgForErp( Organizational org,int type,String accountCode){
        //组织信息
        UnitDto unitDto = new UnitDto();
        unitDto.setID(org.getoID()+"");
        unitDto.setType(1);
        unitDto.setIsStop(0);
        unitDto.setUnitNname(org.getOrganizationalName());
        unitDto.setAccountCode(accountCode);
        unitDto.setIsJoint(1);

        if(type == 0){
            int level = org.getLevel();
            switch (level){
                case 1:
                    unitDto.setpID(null);
                    break;
                case 2:
                    //得到总部id
                    unitDto.setpID(organizationalMapper.selectHeadCCB(SystemContext.getCompanyID()).getoID()+"");
                    break;
                case 3:
                    unitDto.setpID(org.getBranchID()+"");
                    break;
                case 4:
                    unitDto.setpID(org.getTwoLevelBranchID()+"");
                    break;
                default:
                    throw new ServiceException("传递的参数错误", RestConst.ErrorCode.FORM_FAIL);
            }
        }
        //剩下一个父极id

        //这里是调用

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("secretKey",CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.SECRETKEY.getCode()));
        RestTemplate ts = new RestTemplate();

        ResponseEntity<ResultData> result = null;
        HttpEntity<UnitDto> entity = new HttpEntity<UnitDto>(unitDto,headers);

        if(0==type){
            //表示新增
            result = ts.exchange(CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.ADDORGANIZATIONALURL.getCode()), HttpMethod.POST, entity, ResultData.class, "");
        }else{

            result =  ts.exchange(CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.UPDATEORGANIZATIONAL.getCode()), HttpMethod.POST, entity, ResultData.class, "");
        }
        ResultData res =  result.getBody();
        String code = res.getCode();
        if("1".equals(code)){
            //成功
            return 1;
        }else{
            return 0;
        }
    }

    /**
     *删除架构
     * @return
     */
    public Object deleteOrgErp(String oId){
        //String oId
//        String oId = "811183479352987648";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("secretKey",CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.SECRETKEY.getCode()));
        RestTemplate ts = new RestTemplate();
        ResponseEntity<ResultData> result = null;
        HttpEntity<String> entity = new HttpEntity<String>(oId,headers);
        result = ts.exchange(CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.DELETEORGANIZATIONALURL.getCode())+"?unitID="+oId, HttpMethod.GET, entity, ResultData.class, "");
        ResultData res =  result.getBody();
        String code = res.getCode();
        if("1".equals(code)){
            //成功
            return 1;
        }else{
            return 0;
        }
    }



    public Object exportSql(int type,String accountCode){
        //组织信息

        UnitDto[] unitDtos = new UnitDto[20000];
        UnitDto unitDto = null;
        for(int i=0;i<20000;i++){
            unitDto = new UnitDto();
            unitDto.setID(IdGen.get().nextId()+"");
            unitDto.setType(1);
            unitDto.setIsStop(0);
            unitDto.setUnitNname("abcd");
            unitDto.setAccountCode(accountCode);
            unitDto.setIsJoint(1);
            unitDtos[i]=unitDto;
        }
        //剩下一个父极id

        //这里是调用

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("secretKey",CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.SECRETKEY.getCode()));
        RestTemplate ts = new RestTemplate();

        ResponseEntity<ResultData> result = null;
        HttpEntity< UnitDto[]> entity = new HttpEntity< UnitDto[]>(unitDtos,headers);


            //表示新增
            result = ts.exchange(CommonTools.getExtString(restTemplate, ErpInterfaceCfgEnum.MAINORG.getCode()), HttpMethod.POST, entity, ResultData.class, "");



        ResultData res =  result.getBody();
        String code = res.getCode();
        if("1".equals(code)){
            //成功
            return 1;
        }else{
            return 0;
        }
    }



}
